// pages/orderDetail/index.js
const {
  post,
  dateFormat
} = getApp();
Page({
  data: {
    active: 0,
    params: {
      start_time: "",
      end_time: "",
    },
    data: [{
        dataList: [],
        params: {
          page: 1,
          order_status: "",
          pageSize: 20
        },
        showLoading: true
      },
      {
        dataList: [],
        params: {
          page: 1,
          order_status: 2,
          pageSize: 20
        },
        showLoading: true
      },
      {
        dataList: [],
        params: {
          page: 1,
          order_status: 0,
          pageSize: 20
        },
        showLoading: true
      }
    ],
    showPage: false,
    show: false,
    minDate: new Date(2020, 0, 1).getTime(),
    defaultDate: [new Date().getTime(), new Date(new Date().setDate(new Date().getDate() + 1)).getTime()]
  },
  // 初始化加载
  onLoad: function () {
    this.getList();
  },
  // 获取数据
  getList() {
    if (this.data.data[this.data.active].showLoading) {
      post("/order/detail/list", {
        ...this.data.data[this.data.active].params,
        ...this.data.params
      }).then(res => {
        if (res.result == 1) {
          const dataList = this.data.data[this.data.active].dataList.concat(res.data);
          this.setData({
            [`data[${this.data.active}].dataList`]: dataList,
            showPage: true
          });
          if (res.data.length < this.data.data[this.data.active].params.pageSize) {
            this.setData({
              [`data[${this.data.active}].showLoading`]: false
            })
          }
        };
      });
    }
  },
  // 搜索框事件
  searchValue: function (e) {
    this.setData({
      [`data[${this.data.active}].params.page`]: 1,
      [`data[${this.data.active}].dataList`]: [],
      [`data[${this.data.active}].showLoading`]: true
    });
    this.getList();
  },
  // 改变标签
  onchangeActive(e) {
    this.setData({
      active: e.detail.index
    });
    if (this.data.data[e.detail.index].dataList.length == 0) {
      this.getList();
    }
  },
  // 日历选择
  onClose() {
    this.setData({
      show: false
    })
  },
  // 打开日历
  onDisplay() {
    this.setData({
      show: true
    });
  },
  // 确定日历
  onConfirm(e) {
    this.setData({
      "params.start_time": dateFormat(e.detail[0], "YYYY-MM-DD"),
      "params.end_time": dateFormat(e.detail[1], "YYYY-MM-DD"),
      "show": false
    })
  },
  // 上啦加载
  bindscrolltolower: function () {
    this.setData({
      [`data[${this.data.active}].params.page`]: this.data.data[this.data.active].params.page += 1
    });
    this.getList();
  },
  onShow: function () {
    // 控制分享按钮
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline'],
      success(res) {
        console.log('showShareMenu', res);
      }
    });
  }
})